package com.example.seey2023.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.example.seey2023.entity.NcovNews;
import com.example.seey2023.service.NcovNewsService;
import com.example.seey2023.vo.DataView;
import com.example.seey2023.vo.NcovNewsVo;
import org.apache.commons.lang3.StringUtils;
import org.apache.poi.xssf.usermodel.XSSFRow;
import org.apache.poi.xssf.usermodel.XSSFSheet;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

@Controller
@RequestMapping("/news")
public class NcovNewsController {
    @Autowired
    private NcovNewsService ncovNewsService;

    @RequestMapping("/toNews")
    public String toNews(){
        return "news/news";
    }

    @RequestMapping("/listNews")
    @ResponseBody
    public DataView listNews(NcovNewsVo ncovNewsVo){
        QueryWrapper<NcovNews> queryWrapper = new QueryWrapper<>();
        queryWrapper.like(StringUtils.isNotBlank(ncovNewsVo.getTitle()),"title",ncovNewsVo.getTitle());
        IPage<NcovNews> iPage = new Page<>(ncovNewsVo.getPage(),ncovNewsVo.getLimit());
        ncovNewsService.page(iPage,queryWrapper);
        return new DataView(iPage.getTotal(),iPage.getRecords());
    }

    @RequestMapping("/deleteById")
    @ResponseBody
    public DataView deleteById(Integer id){
        ncovNewsService.removeById(id);
        DataView dataView = new DataView();
        dataView.setMsg("删除成功");
        dataView.setCode(200);
        return dataView;
    }

    @RequestMapping("/addOrUpdateNews")
    @ResponseBody
    public DataView addOrUpdate(NcovNews ncovNews){
       // ncovNews.setCreateTime(new Date());
        ncovNewsService.saveOrUpdate(ncovNews);
        DataView dataView = new DataView();
        dataView.setMsg("新增/修改成功");
        dataView.setCode(200);
        return dataView;
    }

    @RequestMapping("/excelImportNews")
    @ResponseBody
    public DataView excelImportNews(@RequestParam("file") MultipartFile file) throws IOException {
        DataView dataView = new DataView();
        // 1.文件不为空
        if (file.isEmpty()) {
            dataView.setMsg("文件为空，不能上传！");
        }
        // 2.POI获取Excel解析数据
        XSSFWorkbook wb = new XSSFWorkbook(file.getInputStream());
        XSSFSheet sheet = wb.getSheetAt(0);
        // 3.定义一个程序集合 接受 文件中的数据
        List<NcovNews> list = new ArrayList<>();
        XSSFRow row = null;
        // 4.解析数据，装到集合里面
        for (int i = 0; i < sheet.getPhysicalNumberOfRows(); i++) {
            // 4.1 定义实体
            NcovNews ncovNews = new NcovNews();
            // 4.2 每一行的数据，放到实体类里面
            row = sheet.getRow(i);
            // 4.3 解析数据
            ncovNews.setTitle(row.getCell(0).getStringCellValue());
            ncovNews.setContent(row.getCell(1).getStringCellValue());
            ncovNews.setCreateTime(row.getCell(2).getDateCellValue());
            ncovNews.setPublishby(row.getCell(3).getStringCellValue());
            // 5.添加list集合
            list.add(ncovNews);
        }
        // 6. 插入数据库
        ncovNewsService.saveBatch(list);
        dataView.setCode(200);
        dataView.setMsg("excel插入成功！");
        return dataView;
    }

}
